package com.kunbo.cn.app.service.impl;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.app.service.SelectService;
import com.kunbo.cn.app.util.EmtryUtil;
import com.kunbo.cn.app.util.OrganizationDTO;

@Service
public class SelectImpl implements SelectService{
	
	@Autowired
	private JdbcTemplate jdbc;
	

	@Override
	public List<Map<String, Object>> noticeType() {
		// TODO Auto-generated method stub
		String sql = "select id,text from select_notice_type";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> waybillNo() {
		// TODO Auto-generated method stub
		String sql = "select * from business_purchase_waybill";
		//List<Map<String, Object>> list = jdbc.queryForList(sql);
		/*if(list != null  && list.size()>0) {
			for (Map<String, Object> map : list) {
				//订单号
				if(map.get("purchase_waybill_source") != null && !map.get("purchase_waybill_source").toString().isEmpty()) {
					String orderCode = "select purchase_order_code from bussiness_purchasing_order_manager where id = " + Integer.parseInt(map.get("purchase_waybill_source").toString());
					if(jdbc.queryForList(orderCode) != null && jdbc.queryForList(orderCode).size() > 0) {
						map.put("purchase_waybill_source", jdbc.queryForList(orderCode).get(0).get("purchase_order_code").toString());
					}
				};
				//供应商
				if(map.get("purchase_waybill_placeoftransportation") != null && !map.get("purchase_waybill_placeoftransportation").toString().isEmpty()) {
					String placeOftransportation = "select name from business_supplier_manage where id = " + Integer.parseInt(map.get("purchase_waybill_placeoftransportation").toString());
					if(jdbc.queryForList(placeOftransportation) != null && jdbc.queryForList(placeOftransportation).size()>0) {
						map.put("purchase_waybill_placeoftransportation", jdbc.queryForList(placeOftransportation).get(0).get("name").toString());
					}
				
				};
				//收货单位
				if(map.get("receving_unit") != null && !map.get("receving_unit").toString().isEmpty()) {
					String receving = "select name from business_customer_manage where id = " + Integer.parseInt(map.get("receving_unit").toString());
					if(jdbc.queryForList(receving) != null && jdbc.queryForList(receving).size()>0){
						map.put("receving_unit", jdbc.queryForList(receving).get(0).get("name").toString());
					}
				}
				
			}
		}*/
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> receivingUnit() {
		// TODO Auto-generated method stub
		String sql  = "select name as id , name as text  from  business_customer_manage ";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> transportUnit() {
		// TODO Auto-generated method stub
		String sql = "select name_of_transport_unit as id ,name_of_transport_unit as text from transport_unit_management";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> carNumber(String id) {
		// TODO Auto-generated method stub
		String unitSql = "select id from transport_unit_management where name_of_transport_unit = '" + id + "'";
		List<Map<String, Object>> list = jdbc.queryForList(unitSql);
		if(list != null  && list.size() > 0) { 
		String sql = "select license_plate_number as id ,license_plate_number as text,maximum_payload from  vehicle_management where subordinate_transport_units = '" + Integer.parseInt(list.get(0).get("id").toString()) +"' and car_state = '空闲'";
		return jdbc.queryForList(sql);
		}
		return null;
	}
	
	@Override
	public List<Map<String, Object>> carDriver() {
		// TODO Auto-generated method stub
		String sql = "select name as id , name as text,nunmber,idcrad from driver_management where driver_state = '空闲'";
		return jdbc.queryForList(sql);
	}

	@Override
	public String orderCode(String wayBillCode) {
		// TODO Auto-generated method stub
		String sql = "select purchase_waybill_source from business_purchase_waybill where purchase_waybill_code = '" + wayBillCode + "'";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		for (Map<String, Object> map : list) {
			sql = "select purchase_order_code from bussiness_purchasing_order_manager id = " + Integer.parseInt(map.get("purchase_waybill_source").toString());
			list = jdbc.queryForList(sql);
			for (Map<String, Object> map2 : list) {
				return map2.get("purchase_order_code").toString();
			}
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> carType() {
		// TODO Auto-generated method stub
		String sql = "select id , number_of_axles as  text ,maximum_load from car_type_manager";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> handlingType() {
		// TODO Auto-generated method stub
		String sql = "select text as id,text from select_zhuangxieleixing";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> saleWayBillNo() {
		String sql = "select * from business_waybill_manage";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getUsers() {
		// TODO Auto-generated method stub
		String sql = "select id,name from business_customer_manage";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> Carrier() {
		String sql = "select * from transport_unit_management";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> CarrierUtype() {
		String sql = "select * from select_carrier_user_type";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> matter() {
		String sql = "select * from business_materials_manage";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> matterid() {
		String sql = "select id,name from business_materials_manage";
		return jdbc.queryForList(sql);
	}
	
	
	@Override
	public List<Map<String, Object>> getSuppliers() {
		// TODO Auto-generated method stub
		String sql = "select id,name from business_supplier_manage";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getUserTypes() {
		// TODO Auto-generated method stub
		String sql = "select * from select_select_customer_users_type";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getSupplierType() {
		// TODO Auto-generated method stub
		String sql = "select * from select_supplier_user_type";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>> wellhead() {
		String sql = "select * from select_wellhead";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<OrganizationDTO> getOrganization() {
		// TODO Auto-generated method stub
		List<OrganizationDTO> lists = new ArrayList<OrganizationDTO>();
		String sql  = "select DISTINCT(p.title ) as pnode,p.id from business_organization_manage  p , business_organization_manage s where  s.pid = p.id";
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		if(list != null) {
			for (Map<String, Object> map : list) {
				OrganizationDTO dto = new OrganizationDTO();
				dto.setTitle(map.get("pnode").toString());
				String childSql = "select  title from business_organization_manage   where  pid = '" + map.get("id").toString() +"'";
				dto.setChildren(jdbc.queryForList(childSql));
				lists.add(dto);
			}
		}
		return lists;
	}
	@Override
	public List<Map<String, Object>>getSupplier(){
		String sql = "select id,name,supplier_code from business_supplier_manage";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>>getMatterType(){
		String sql = "select id,name from business_matter_type_manage";
		return jdbc.queryForList(sql);
	}
	@Override
	public List<Map<String, Object>>getOrganizations(){
		String sql = "select id,title from business_organization_manage";
		return jdbc.queryForList(sql);
	}



	
	@Override
	public List<Map<String, Object>> plateNumber() {
		String Sql = "select id,license_plate_number  from vehicle_management";
		return jdbc.queryForList(Sql);
	}
	@Override
	public List<Map<String, Object>> carDrivers() {
		// TODO Auto-generated method stub
		String sql = "select id,name,idcrad from driver_management";
		return jdbc.queryForList(sql);
	}
	public static void main(String[] args) {
        
	}
	
	@Override
	public List<Map<String, Object>> getMatter(String mater_type) {
		String sql = "select id,name from business_materials_manage where mater_type='"+mater_type+"' ";
		return jdbc.queryForList(sql);
	}

	@Override
	public List<Map<String, Object>> getAllCar(String carNumber) {
		String sql = "select license_plate_number,vehicle_type,maximum_payload from vehicle_management where car_state = '空闲'  ";
		if(StringUtils.isNotBlank(carNumber)) {
			sql+= " and license_plate_number like '%"+carNumber+"%'";
		}
		return jdbc.queryForList(sql);
	}

}
